<?php


namespace app\seller\controller;

use think\Db;


class Recharge extends Base
{
    public function index()
    {
        $length = input('length');
        $list = Db::name('recharge_rule')->where('category_id', $this->shop_id)->limit($length, 30)->select();
        return $this->success('获取成功', $list);
    }

    public function edit()
    {
        $id = input('id');
        $data = Db::name('recharge_rule')->where('category_id', $this->shop_id)->where('id', $id)->find();
        return $this->success('获取成功', $data);
    }

    public function add()
    {
        $amount = floatval(input('amount'));
        $give_amount = floatval(input('give_amount'));
        $remark = input('remark');
        $level = intval(input('level'));
        $discount = floatval(input('discount'));
        if (!$give_amount) {
//            return $this->error('请输入赠送金额');
        }
        if (!$remark) {
            return $this->error('请输入说明');
        }
        if ($level == 0) {
            $discount = 10;
        } else {
            if (Db::name('recharge_rule')->where('category_id', $this->shop_id)->where('level', $level)->count()) {
                return $this->error('级别已存在');
            }
        }
        $res = Db::name('recharge_rule')->insert([
            'category_id' => $this->shop_id,
            'amount' => $amount,
            'give_amount' => $give_amount,
            'remark' => $remark,
            'discount' => $discount,
            'level' => $level,
        ]);
        if ($res !== false) {
            return $this->success('操作成功');
        }
        return $this->error('操作失败');
    }

    public function del()
    {
        $id = input('id');
        $res = Db::name('recharge_rule')->where([
            'id' => $id,
            'category_id' => $this->shop_id,
        ])->delete();
        if ($res !== false) {
            return $this->success('操作成功');
        }
        return $this->error('操作失败');
    }

    public function doedit()
    {
        $id = input('id');
        $amount = floatval(input('amount'));
        $give_amount = floatval(input('give_amount'));
        $remark = input('remark');
        $level = intval(input('level'));
        $discount = floatval(input('discount'));
        if (!$give_amount) {
//            return $this->error('请输入赠送金额');
        }
        if (!$remark) {
            return $this->error('请输入说明');
        }
        if ($level == 0) {
            $discount = 10;
        } else {
            if (Db::name('recharge_rule')->where('category_id', $this->shop_id)->where('level', $level)->where('id', "<>", $id)->count()) {
                return $this->error('级别已存在');
            }
        }
        $res = Db::name('recharge_rule')->where([
            'id' => $id,
            'category_id' => $this->shop_id,
        ])->update([
            'amount' => $amount,
            'give_amount' => $give_amount,
            'remark' => $remark,
            'discount' => $discount,
            'level' => $level,
        ]);
        if ($res !== false) {
            return $this->success('操作成功');
        }
        return $this->error('操作失败');
    }
}
